package edu.hawaii.wicket.page;

import java.util.Map;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;

import edu.hawaii.wicket.page.energy.EnergyPage;
import edu.hawaii.wicket.page.home.HomePage;
import edu.hawaii.wicket.page.scheduler.SchedulerPage;
import edu.hawaii.wicket.page.style.IEStylesheetHeaderContributor;
import edu.hawaii.wicket.page.util.UtilPage;
import edu.hawaii.wicket.ExampleApplicationSession;

import java.util.TimeZone;

import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.util.time.Duration;

/**
 * A Base page that serves as the superclass for all pages in this application.
 * The four pages (HomePage, ListPage, FormPage, and TablePage) all inherit from this class.
 * This class also adds the CSS stylesheets that will be used by all pages. 
 *  
 * @author Philip Johnson
 *
 */
public abstract class BasePage extends WebPage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  private static final ResourceReference RESOURCE_REF = new ResourceReference(BasePage.class,
  "web-bar.png");
  private static final ResourceReference WIDGET = new ResourceReference(BasePage.class,
  "widget.png");
  private static final ResourceReference M1_REF = new ResourceReference(BasePage.class,
  "m1_active.png");
  private static final ResourceReference M2_REF = new ResourceReference(BasePage.class,
  "m2_active.png");
  private static final ResourceReference M3_REF = new ResourceReference(BasePage.class,
  "m3_active.png");
  private static final ResourceReference M4_REF = new ResourceReference(BasePage.class,
  "m4_active.png");
  /**
   * The base page layout used by all pages. 
   * This includes a title and the links that appear in the tabbed menu. 
   */
  public BasePage() {
    // Add CSS definitions for use in all pages
    add(CSSPackageResource.getHeaderContribution(
        edu.hawaii.wicket.page.BasePage.class, "style/blueprint/screen.css", "screen"));
    add(CSSPackageResource.getHeaderContribution(
        edu.hawaii.wicket.page.BasePage.class, "style/blueprint/print.css", "print"));
    add(new IEStylesheetHeaderContributor(
        new ResourceReference(edu.hawaii.wicket.page.BasePage.class, "style/blueprint/ie.css")));
    
    add(new Label("title", "Solar Decathlon Home Management System"));
    
    // Now add the links that go in the tabs. 
    add(new Link<String>("HomePageLink") {
      private static final long serialVersionUID = 1L;
      /** Upon clicking this link, go to HomePage. */
      @Override
      public void onClick() {
        setResponsePage(new EnergyPage());
      }
      }
    );
    
    add(new Link<String>("UtilitesPageLink") {
      private static final long serialVersionUID = 1L;
      /** Upon clicking this link, go to ListPage. */
      @Override
      public void onClick() {
        setResponsePage(new UtilPage());
      } 
      }
    );
    
    add(new Link<String>("FormPageLink") {
      private static final long serialVersionUID = 1L;
      /** Upon clicking this link, go to FormPage. */
      @Override
      public void onClick() {
        setResponsePage(new HomePage());
      }
      }
    );
    
    add(new Link<String>("SchedulerPageLink") {
      private static final long serialVersionUID = 1L;
      /** Upon clicking this link, go to TablePage. */
      @Override
      public void onClick() {
        setResponsePage(new SchedulerPage());
      }
      }
    );
    add(new Image("widget", new Model<ResourceReference>(WIDGET)));
    add(new Image("m1", new Model<ResourceReference>(M1_REF)));
    add(new Image("m2", new Model<ResourceReference>(M2_REF)));
    add(new Image("m3", new Model<ResourceReference>(M3_REF)));
    add(new Image("m4", new Model<ResourceReference>(M4_REF)));
    // add the clock component
    Clock clock = new Clock("clock", TimeZone.getTimeZone("US/Hawaii"));
    add(clock);

    // add the ajax behavior which will keep updating the component every 5
    // seconds
    clock.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(5)));
  }
  
  
  /**
   * Returns the property map associated with this user's session.
   * @return The session's property map.
   */
  public Map<String, String> getSessionProperties() {
    return ((ExampleApplicationSession)getSession()).getProperties();
    
  }
}
